Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
driver_systick.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file driver_systick.c
31  \brief Encapsulates the ARM sysTick counter, which is used for benchmarking
32 */
33 
34 #include "sensor_fusion.h"
35 #include "drivers.h"
36 
37 // SysTick register definitions based on CMSIS definitions for same
38 #define SYST_CSR SysTick->CTRL // SysTick Control & Status Register
39 #define SYST_RVR SysTick->LOAD // SysTick Reload Value Register
40 #define SYST_CVR SysTick->VAL // SysTick Current Value Register
41 
42 // ARM-core specific function that enables the ARM systick timer on Kinetis uCs.
43 // this is purely for algorithm benchmarking and can either be deleted or re-written for use on other uCs.
44 // the systick clock frequency for each uC are defined as CORE_SYSTICK_HZ in types.h.
45 
46 // the timer is 24 bit so allows measurement of intervals up to 2^24/CORE_SYSTICK_HZ secs=0.35s for a 48MHz uC.
48 {
49  SYST_CSR = 0x5u; // enable systick from internal clock
50  SYST_RVR = 0x00FFFFFFu; // set reload to maximum 24 bit value
51  return;
52 }
53 
54 // ARM-core specific function to store the current systick timer ticks for benchmarking
56 {
57  // store the 24 bit systick timer
58  *pstart = SYST_CVR & 0x00FFFFFF;
59 
60  return;
61 }
62 
63 // ARM-core specific function to compute the elapsed systick timer ticks for benchmarking
65 {
66  int32 elapsed_ticks;
67 
68  // subtract the stored start ticks and check for wraparound down through zero
69  elapsed_ticks = start_ticks - (SYST_CVR & 0x00FFFFFF);
70  if (elapsed_ticks < 0) elapsed_ticks += SYST_RVR;
71 
72  return elapsed_ticks;
73 }
74 
75 void ARM_systick_delay_ms(uint32 iSystemCoreClock, uint32 delay_ms)
76 {
77  int32 istart_ticks; // start ticks on entry
78  int32 ielapsed_ticks; // elapsed ticks
79  int16 i; // loop counter
80 
81  // loop for requested number of ms
82  for (i = 0; i < delay_ms; i++)
83  {
84  // loop until 1ms has elapsed
85  ARM_systick_start_ticks(&istart_ticks);
86  do
87  {
88  ielapsed_ticks = ARM_systick_elapsed_ticks(istart_ticks);
89  } while (ielapsed_ticks < iSystemCoreClock / 1000);
90  }
91 
92  return;
93 }
void ARM_systick_enable(void)
int32_t int32
Definition: sensor_fusion.h:57
uint32_t uint32
Definition: sensor_fusion.h:60
int32 ARM_systick_elapsed_ticks(int32 start_ticks)
The sensor_fusion.h file implements the top level programming interface.
Provides function prototypes for driver level interfaces.
#define SYST_RVR
#define SYST_CSR
#define SYST_CVR
void ARM_systick_delay_ms(uint32 iSystemCoreClock, uint32 delay_ms)
int16_t int16
Definition: sensor_fusion.h:56
void ARM_systick_start_ticks(int32 *pstart)